package com.hzw.saas.web.storage.controller;

import com.hzw.saas.api.storage.IThumbnailService;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.service.storage.service.ResourceMigrateService;
import com.hzw.saas.web.storage.param.ExecTaskParam;
import com.hzw.saas.web.storage.param.FileMigrationParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Objects;

/**
 * @author zzl
 * @since 10/11/2021
 */
@Controller
@RequiredArgsConstructor
@Slf4j
@Api(tags = "任务管理/资源任务")
public class UserResourceTaskController {

    private final ResourceMigrateService migrateService;

    private final IThumbnailService thumbnailService;

    private static final String TASK_MIGRATION_PASSWORD = "999999";

    @PostMapping("/task/migration/")
    @SysLog(operation = "文件迁移", mode = 3)
    @ApiOperation(value = "文件迁移", notes = "迁移用户文件，冷数据迁移到S3，热数据迁移到新的存储目录(相关参数有效时)")
    public ResponseEntity<String> startMigrateTotalTask(@RequestBody FileMigrationParam param) {
        AssertUtil.assertThrow("任务密码错误", !Objects.equals(param.getPassword(), TASK_MIGRATION_PASSWORD));
        migrateService.migrateFiles2CloudAnd2NewCatalog(null, param.getGeDate(), param.getLtDate(),
            param.getBefore2Cloud(), param.getNewCatalog(), param.getThreadCount());
        return ResponseEntity.ok("任务已启动...");
    }


    @PostMapping("/task/separate-ds/")
    @SysLog(operation = "DS数据分离", mode = 3)
    @ApiOperation(value = "DS数据分离", notes = "分录数据库中的Hbox数据与DS数据")
    public ResponseEntity<String> startSeparateDsTask(@RequestBody ExecTaskParam param) {
        AssertUtil.assertThrow("任务密码错误", !Objects.equals(param.getPassword(), TASK_MIGRATION_PASSWORD));
        migrateService.separateHboxAndDS(param.getThreadCount());
        return ResponseEntity.ok("任务已启动...");
    }

    @PostMapping("/task/generate-thumbnails/")
    @SysLog(operation = "创建图片缩略图", mode = 3)
    @ApiOperation(value = "创建图片缩略图", notes = "创建的图片缩略图（执行对象为缺少缩略图的图片）")
    public ResponseEntity<String> generateThumbnail(@RequestBody ExecTaskParam param) {
        AssertUtil.assertThrow("任务密码错误", !Objects.equals(param.getPassword(), TASK_MIGRATION_PASSWORD));
        thumbnailService.execThumbnailTask(param.getThreadCount());
        return ResponseEntity.ok("任务已启动...");
    }

}
